<?php
/**
* 发货单
*
* @copyright shopex.cn 2015.05.07
* @author sunjing<sunjing@shopex.cn>
*/
class middleware_wms_matrixwms_request_qimen_delivery extends middleware_wms_matrixwms_request_delivery
{
    private $_shop_type_mapping = array(
        'taobao'    => 'TB',
        'paipai'    => 'PP',
        '360buy'    => 'JD',
        'yihaodian' => 'YHD',
        'qq_buy'    => 'QQ',
        'dangdang'  => 'DD',
        'amazon'    => 'AMAZON',
        'yintai'    => 'YT',
        'vjia'      => 'FK',
        'alibaba'   => '1688',
        'suning'    => 'SN',
        'gome'      => 'GM',
        'mogujie'   => 'MGJ',
    );

    public function delivery_cancel(&$sdf,$sync=false){

        $delivery_bn = $sdf['outer_delivery_bn'];
        $oDelivery_extension = app::get('console')->model('delivery_extension');
        $dextend = $oDelivery_extension->dump(array('delivery_bn'=>$delivery_bn)); 

        $params = array(
            'order_type'     => 'OUT_SALEORDER',
            'out_order_code' => $delivery_bn,
            'warehouse_code' => $this->getWarehouse_code($sdf['branch_bn']),
            'order_id'   => $dextend['original_delivery_bn']
        );

        $writelog = array(
            'log_title'   => '发货单取消',
            'log_type'    => 'store.trade.delivery',
            'original_bn' => $delivery_bn,
        );

        $method = 'store.wms.order.cancel';
        return $this->request($method,$params,$writelog,$sync);
    }

    protected function __getDelivery_create_params($sdf){

        $params = parent::__getDelivery_create_params($sdf);

        $params['warehouse_code'] = $this->getWarehouse_code($sdf['branch_bn']);

        // 发货人信息
        $shop = app::get('ome')->model('shop')->dump($sdf['shop_id']);
        $area = explode(':',$shop['area']);
        list($shipper_state,$shipper_city,$shipper_address) = explode('/',$area[1]);

        $params['shipper_name']    = $shop['default_sender'];
        $params['shipper_mobile']  = $shop['mobile'];
        $params['shipper_state']   = $shipper_state;
        $params['shipper_city']    = $shipper_city;
        $params['shipper_address'] = $shipper_address;
        $params['order_flag']      = $params['is_cod'] == 'true' ? 'COD' : '';
        $params['order_source']    = $this->_shop_type_mapping[$sdf['shop_type']]  ?  $this->_shop_type_mapping[$sdf['shop_type']] : 'OTHER';
        $params['operate_time']    = date('Y-m-d H:i:s');
        
        return $params;
    }

    public function delivery_create_callback($callback_result,$callback_params){
        $rsp = $this->callback($callback_result,$callback_params);
        $status = $callback_result['rsp'];
        if($status == 'succ'){
            $api_status = 'success';
        }else{
            $api_status = 'fail';
        }
        $log_id = $callback_params['log_id'];
        $log_detail = $this->getLogDetail(array('log_id'=>$log_id), 'msg_id,params,original_bn,addon');
        $delivery_bn = $log_detail['original_bn'];
        if ($delivery_bn) {
            $deliveryObj = app::get('ome')->model('delivery');
            $deliverys = $deliveryObj ->dump(array('delivery_bn'=>$delivery_bn),'delivery_id');
            $msg = $callback_result['err_msg'] ? $callback_result['err_msg'] : $callback_result['res'];
            app::get('console')->model('delivery_send')->update_send_status($deliverys['delivery_id'],$api_status,$msg);

            $data = @json_decode($callback_result['data'],true);

            if ($data['wms_order_code']) {
                $oDelivery_extension = app::get('console')->model('delivery_extension');
                $ext_data['original_delivery_bn'] = $data['wms_order_code'];
                $ext_data['delivery_bn']          = $delivery_bn;
                $oDelivery_extension->save($ext_data);
            }
        }
        
        return $rsp;
    }
}